package com.company.genealogy.web.service.impl;

import com.company.genealogy.web.mapper.UserContributionMapper;
import com.company.genealogy.web.service.UserContributionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户贡献服务实现
 */
@Service
public class UserContributionServiceImpl implements UserContributionService {
    
    @Autowired
    private UserContributionMapper userContributionMapper;
    
    @Override
    @Transactional
    public boolean addContribution(Long userId, Long clanId, String type, String content, Integer points) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("userId", userId);
            params.put("clanId", clanId);
            params.put("type", type);
            params.put("content", content);
            params.put("points", points);
            
            return userContributionMapper.insertContribution(params) > 0;
        } catch (Exception e) {
            // 记录日志但不影响业务
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    public List<Map<String, Object>> getTopContributors(Long clanId, int limit) {
        return userContributionMapper.getTopContributors(clanId, limit);
    }
    
    @Override
    public Map<String, Object> getUserContribution(Long userId, Long clanId) {
        return userContributionMapper.getUserContribution(userId, clanId);
    }
} 